package top.drinyeit.weilong.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.base.BaseActivity;
import top.drinyeit.weilong.presenter.LoginPresenter;

/**
 * Created by bigHead on 2017/11/14.
 */

public class LoginActivity extends BaseActivity {

    private RelativeLayout login_tit;
    private ImageView login_back;
    private TextView registration;

    private TextView phone_number;//手机号
    private TextView get_code;//获取验证码
    private TextView code;//验证码
    private TextView login;//登录

    private LoginPresenter loginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_login);
        registration = findViewById(R.id.tv_registration);
        phone_number = findViewById(R.id.phone_number);
        get_code = findViewById(R.id.get_code);
        code = findViewById(R.id.code);
        login = findViewById(R.id.login);

        initData();
        init();
        setTitle(true, "登录");
    }

    private void initData() {
        loginPresenter = new LoginPresenter(this);
    }

    @Override
    public void init() {
        get_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginPresenter.getCode(phone_number.getText().toString());
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginPresenter.login(phone_number.getText().toString(), code.getText().toString());
            }
        });
        registration.setVisibility(View.VISIBLE);

        registration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegistrationActivity.class);
                startActivity(intent);
            }
        });
    }

    Timer timer;
    TimerTask timerTask;
    int s = 60;

    //验证码倒计时
    public void setTime() {
        get_code.setEnabled(false);
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                if (s > 0) {
                    get_code.post(new Runnable() {
                        @Override
                        public void run() {
                            String str = --s + "秒后获取";
                            get_code.setText(str);
                        }
                    });
                } else {
                    if (timerTask != null) {
                        timerTask.cancel();
                    }
                    s = 60;
                    get_code.post(new Runnable() {
                        @Override
                        public void run() {
                            String str = "获取验证码";
                            get_code.setText(str);
                            get_code.setEnabled(true);
                        }
                    });
                }
            }
        };
        timer.schedule(timerTask, 0, 1000);
    }
}
